package com.xh.demo.mq.stream;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.xh.demo.mq.stream.source.DelayMessageOutputSource;
import com.xh.demo.mq.stream.source.consts.NoticeMessageConst;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author wen
 * @apiNote 消息通知MQ生产者
 * @since 2021-05-31
 */
@Slf4j
@Component
@EnableBinding({DelayMessageOutputSource.class})
public class DelayNoticeProducer {

    @Autowired
    private DelayMessageOutputSource delayMessageOutputSource;

    /**
     * 发送延时消息
     */
    public void sendDelayedMsg(String message, long delayTime) {
        delayMessageOutputSource.output().send(MessageBuilder.withPayload(message)
                .setHeader(NoticeMessageConst.DELAY_MESSAGE_HEADER, delayTime)
                .build());
        log.error("消息通知-STREAM-DLX交换机-发送延时消息-sendMsg【{}】【{}】【{}】", DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN), delayTime, message);
    }

    /**
     * 发送普通消息
     */
    public void sendNormalMsg(String message) {
        delayMessageOutputSource.output().send(MessageBuilder.withPayload(message).build());
        log.error("消息通知-STREAM-DLX交换机-发送普通消息-sendMsg【{}】【{}】", DateUtil.format(LocalDateTime.now(), DatePattern.NORM_DATETIME_PATTERN), message);
    }

}
